package com.leyou.item.service;


import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> queryListByParent(Long pid) {
        //查询条件，mapper会把对象中的非空属性作为查询条件
        Category t = new Category();
        t.setParentId(pid);
        List<Category> list = categoryMapper.select(t);
        //判断结果
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //使用自定义工具类，把Category集合转换为DTO的集合
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }

    /**
     *
     * @param id
     * @return
     */
    public List<CategoryDTO> queryByBrandId(Long id) {
        //判断结果
        List<Category> categories = categoryMapper.queryByBrandId(id);
        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        List<CategoryDTO> categoryDTOS = BeanHelper.copyWithCollection(categories, CategoryDTO.class);
        // 使用自定义工具类，把Category集合转为DTO的集合
        return categoryDTOS;
    }

    public List<CategoryDTO> queryByIds(List<Long> categoryIds) {
        System.out.println(categoryIds);
        List<Category> categories = categoryMapper.selectByIdList(categoryIds);
        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }


        return BeanHelper.copyWithCollection(categories,CategoryDTO.class);
    }
}
